using DotNetty.Buffers;
using QQ.Client;
using QQ.Codecs;
using static QQ.Codecs.DotNettyByteBufferExtensions.LengthType;
namespace QQ.Packet.Request
{
    public class SSOFrame : IRequest
    {
        private readonly QQDeviceConfig config;
        public SSOFrame(QQDeviceConfig config)
        {
            this.config = config;
        }
        public string cmdName { get; set; }
        private int sequenceId = 85600;
        private int SequenceId { get => sequenceId += 2; set { } }
        public IByteBuffer body { get; set; }
        public IByteBuffer extraData { get; set; }
        public IByteBuffer Serialize()
        {
            IByteBuffer header = Unpooled.Buffer()
                .WriteInt(SequenceId)
                .WriteInt((int)config.subAppId)
                .WriteInt((int)config.subAppId)
                .WriteBytes("01 00 00 00 00 00 00 00 00 00 01 00".HexStringToBytes())
                .WriteBytesWithLength(extraData == null ? Unpooled.Buffer() : extraData, INT, lengthOffset: 4)
                .WriteBytesWithLength(cmdName.ToByteBuffer(), INT, lengthOffset: 4)
                .WriteBytesWithLength("02 B0 5B 8B".HexStringToByteBuffer(), INT, lengthOffset: 4)
                .WriteBytesWithLength(config.IMEI.ToByteBuffer(), INT, lengthOffset: 4)
                .WriteInt(4)
                .WriteBytesWithLength(config.ksid.ToByteBuffer(), SHORT, lengthOffset: 2)
                .WriteInt(4);
            extraData = null;
            return Unpooled.Buffer()
                .WriteBytesWithLength(header, INT, lengthOffset: 4)
                .WriteBytesWithLength(body, INT, lengthOffset: 4);
        }
    }
}